import os
from dotenv import load_dotenv
import yaml
from pathlib import Path


load_dotenv()  # 只需一次加载即可

# 配置文件路径
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
config_path = Path(base_dir) / "config" / "settings.yaml"

class Config:
    def __init__(self):
        pass

    @property
    def DB_CONFIG(self):
        return {
            "host": os.getenv("DB_HOST", "localhost"),
            "user": os.getenv("DB_USER", "root"),
            "password": os.getenv("DB_PASS", ""),
            "database": os.getenv("DB_NAME", ""),
            "port": int(os.getenv("DB_PORT", "")),
            "engine": os.getenv("DB_ENGINE", "mysql+pymysql")
        }

    @property
    def YT_API_KEYS(self):
        api_keys_str = os.getenv("YT_API_KEYS", "")
        return [key.strip() for key in api_keys_str.split(",")] if api_keys_str else []

    @property
    def DISCORD_BOT_TOKEN(self):
        return os.getenv("DISCORD_BOT_TOKEN")

    @property
    def DEEPSEEK_API_KEY(self):
        return os.getenv("DEEPSEEK_API_KEY")
    
    @property
    def OPENAI_API_KEY(self):
        return os.getenv("OPENAI_API_KEY")
    
    @property
    def HTTP_PROXY(self):
        return os.getenv("HTTP_PROXY")
    
    @property
    def HTTPS_PROXY(self):
        return os.getenv("HTTPS_PROXY")
    

def load_config(config_file: str = "settings.yaml") -> dict:
    """
    从指定路径加载 YAML 格式的配置文件。
    
    :param config_file: 配置文件名，默认为 settings.yaml
    :return: 包含配置内容的字典
    """
    config_path = Path(base_dir) / "config" / config_file

    if not config_path.exists():
        raise FileNotFoundError(f"❌ 未找到配置文件: {config_path}")

    try:
        with open(config_path, "r", encoding="utf-8") as f:
            return yaml.safe_load(f)
    except Exception as e:
        raise RuntimeError(f"❌ 读取配置文件失败: {str(e)}")